#include "stdafx.h"
#include "Player.h"
#include "RectStatic.h"
#include "IniRead.h"
#include "PFMEngine.h"

Player::Player()
:m_nDrawPosX(0)
,m_nDrawPosY(0)
{
	for( int i = 0; i < PART_MAX; ++i )
	{
		m_pRectPart[i] = NULL;
	}
}

Player::~Player()
{
	Release();	
}

void Player::init()
{
	Release();

	m_pRectPart[PART_BODY] = new RectStatic;	
	m_pRectPart[PART_BODY]->Create(GetPFMEngine().CreateRect(NULL), 0, 0, 0, 0);

	m_pRectPart[PART_CLOTH]= new RectStatic;
	m_pRectPart[PART_CLOTH]->Create(GetPFMEngine().CreateRect(NULL), 0, 0, 0, 0);

	m_pRectPart[PART_HEAD] = new RectStatic;
	m_pRectPart[PART_HEAD]->Create(GetPFMEngine().CreateRect(NULL), 0, 0, 0, 0);

	m_pRectPart[PART_HAIR] = new RectStatic;
	m_pRectPart[PART_HAIR]->Create(GetPFMEngine().CreateRect(NULL), 0, 0, 0, 0);
}

void Player::Release()
{
	for( int i = 0; i < PART_MAX; ++i )
	{
		SAFE_DELETE(m_pRectPart[i]);
	}
}

void Player::Create(int nStageIndex, int nDrawPosX, int nDrawPosY)
{
	int nPosX, nPosY, nWidth, nHeight;
	char szResourceName[MAX_PATH] = {0,};
	INI_TYPE eIniIndex = (INI_TYPE)(INI_TYPE_STAGE_1 + nStageIndex);

	nPosX   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Body_OffsetX");
	nPosY   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Body_OffsetY");
	nWidth  = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Body_Width");
	nHeight = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Body_Height");	
	GetIniReader().GetIniKeyValue_String(eIniIndex , szResourceName, MAX_PATH, "PLAYER_INFO", "Body_Img");	
	m_pRectPart[PART_BODY]->SetPosSize(nDrawPosX + nPosX, nDrawPosY + nPosY, nWidth, nHeight);
	m_pRectPart[PART_BODY]->SetTexture(szResourceName);
	m_nPartBaseOffsetX[PART_BODY] = nPosX;
	m_nPartBaseOffsetY[PART_BODY] = nPosY;
	m_nPartBaseWidth[PART_BODY] = nWidth;
	m_nPartBaseHeight[PART_BODY] = nHeight;

	nPosX   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Cloth_OffsetX");
	nPosY   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Cloth_OffsetY");
	nWidth  = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Cloth_Width");
	nHeight = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Cloth_Height");	
	GetIniReader().GetIniKeyValue_String(eIniIndex , szResourceName, MAX_PATH, "PLAYER_INFO", "Cloth_Img");	
	m_pRectPart[PART_CLOTH]->SetPosSize(nDrawPosX + nPosX, nDrawPosY + nPosY, nWidth, nHeight);
	m_pRectPart[PART_CLOTH]->SetTexture(szResourceName);
	m_nPartBaseOffsetX[PART_CLOTH] = nPosX;
	m_nPartBaseOffsetY[PART_CLOTH] = nPosY;
	m_nPartBaseWidth[PART_CLOTH] = nWidth;
	m_nPartBaseHeight[PART_CLOTH] = nHeight;

	nPosX   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Head_OffsetX");
	nPosY   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Head_OffsetY");
	nWidth  = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Head_Width");
	nHeight = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Head_Height");	
	GetIniReader().GetIniKeyValue_String(eIniIndex , szResourceName, MAX_PATH, "PLAYER_INFO", "Head_Img");	
	m_pRectPart[PART_HEAD]->SetPosSize(nDrawPosX + nPosX, nDrawPosY + nPosY, nWidth, nHeight);
	m_pRectPart[PART_HEAD]->SetTexture(szResourceName);
	m_nPartBaseOffsetX[PART_HEAD] = nPosX;
	m_nPartBaseOffsetY[PART_HEAD] = nPosY;
	m_nPartBaseWidth[PART_HEAD] = nWidth;
	m_nPartBaseHeight[PART_HEAD] = nHeight;

	nPosX   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Hair_OffsetX");
	nPosY   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Hair_OffsetY");
	nWidth  = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Hair_Width");
	nHeight = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "PLAYER_INFO", "Hair_Height");	
	GetIniReader().GetIniKeyValue_String(eIniIndex , szResourceName, MAX_PATH, "PLAYER_INFO", "Hair_Img");	
	m_pRectPart[PART_HAIR]->SetPosSize(nDrawPosX + nPosX, nDrawPosY + nPosY, nWidth, nHeight);
	m_pRectPart[PART_HAIR]->SetTexture(szResourceName);
	m_nPartBaseOffsetX[PART_HAIR] = nPosX;
	m_nPartBaseOffsetY[PART_HAIR] = nPosY;
	m_nPartBaseWidth[PART_HAIR] = nWidth;
	m_nPartBaseHeight[PART_HAIR] = nHeight;

	m_nDrawPosX = nDrawPosX;
	m_nDrawPosY = nDrawPosY;
}

void Player::Update()
{
	for( int i = 0; i < PART_MAX; ++i )
	{
		m_pRectPart[i]->Update();
	}
}

void Player::SetPos(int nPosX, int nPosY)
{
	int nOffsetX = nPosX - m_nDrawPosX;
	int nOffsetY = nPosY - m_nDrawPosY;

	if( nOffsetX == 0 && nOffsetY == 0 )	return;

	for( int i = 0; i < PART_MAX; ++i )
	{
		m_pRectPart[i]->SetPos(m_pRectPart[i]->GetPosX() + nOffsetX, m_pRectPart[i]->GetPosY() + nOffsetY);
	}	
	m_nDrawPosX = nPosX;
	m_nDrawPosY = nPosY;
}

void Player::SetScale(float fScale)
{
	for( int i = 0; i < PART_MAX; ++i )
	{
		int nNewPosX = int(m_nDrawPosX + m_nPartBaseOffsetX[i] * fScale);
		int nNewPosY = int(m_nDrawPosY + m_nPartBaseOffsetY[i] * fScale);
		int nWidth = int(m_nPartBaseWidth[i] * fScale);
		int nHeight = int(m_nPartBaseHeight[i] * fScale);
		m_pRectPart[i]->SetPosSize(nNewPosX, nNewPosY, nWidth, nHeight);
	}	
}